from selenium.common.exceptions import NoSuchElementException


def is_element_present(browser, how, what):
    try:
        browser.find_element(by=how, value=what)
    except NoSuchElementException:
        return False
    return True


def verify_list_incremented(old_list, new_list, value):
    list_diff = __diff(new_list, old_list)
    assert(len(list_diff) == 1)
    assert(list_diff.pop() == value)


def verify_list_decremented(old_list, new_list, value):
    list_diff = __diff(old_list, new_list)
    assert(len(list_diff) == 1)
    assert(list_diff.pop() == value)


def verify_list_not_changed(old_list, new_list):
    assert(len(__diff(old_list, new_list)) == 0)
    assert(len(__diff(new_list, old_list)) == 0)


def __diff(a, b):
        b = set(b)
        return [aa for aa in a if aa not in b]